package controller;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.utils.Array;


import model.Block;
import model.World;
import screens.GameMainScreen.StateGame;
public class WorldController {
	
	StateGame stateGame=StateGame.GAMERUNNING;
	World world;
	BobController bobControl;
	FireController fireControl;
	Music musicBackGround;
	public WorldController(World world){
		this.world= world;
		initMusic();
		bobControl = new BobController(this);
		fireControl=new FireController(this);
	}
	public void initMusic(){
		musicBackGround = Gdx.audio.newMusic(Gdx.files.internal(world.getLevel().getMusic()));
		musicBackGround.setLooping(true);
		musicBackGround.play();
	}
	public void update(float delta){
		
		bobControl.update(delta);
		fireControl.update(delta);
		
	}
	public void musicUpdate(){
		if(musicBackGround.isPlaying()){
			musicBackGround.stop();
		}
		musicBackGround=Gdx.audio.newMusic(Gdx.files.internal(world.getLevel().getMusic()));
		musicBackGround.play();
	}
	public void stopMusic(){
		if(musicBackGround.isPlaying()){
			musicBackGround.stop();
		}
	}
	public void setMusic(int i){
		if(musicBackGround.isPlaying()){
			musicBackGround.stop();
		}
		switch( i){
			case 1: musicBackGround=Gdx.audio.newMusic(Gdx.files.internal(world.getLevel().getMusic()));
					musicBackGround.setLooping(true);
					break;
			case 2: musicBackGround=Gdx.audio.newMusic(Gdx.files.internal("music/endgame.mp3"));
					musicBackGround.setLooping(false);
					break;
		}
	}
	public StateGame getStateGame(){
		return stateGame;
	}
	public void setState(StateGame state){
		stateGame=state;
	}
	public World getWorld(){
		return world;
	}
	public BobController getBobControl(){
		return bobControl;
	}
	public void leftPressed(){
		bobControl.leftPressed();
	}
	public void rightPressed(){
		bobControl.rightPressed();
	}
	public void jumpPressed(){
		bobControl.jumpPressed();
	}
	public void firePressed(){
		bobControl.firePressed();
	}
	public void leftReleased(){
		bobControl.leftReleased();
	}
	public void rightReleased(){
		bobControl.rightReleased();
	}
	public void jumpReleased(){
		bobControl.jumpReleased();
	}
	public void fireReleased(){
		bobControl.fireReleased();
	}
	public Array<Block> populateCollidableBlocks(int startX,int startY, int endX, int endY){
		Array<Block> collis = new Array<Block>();
		//collidable.clear();
		for(int x= startX; x<=endX;x++){
			for(int y = startY;y<=endY;y++){
				if(x>=0&& x<world.getLevel().getWidth()&&y>=0&&y<world.getLevel().getHeight()){
					//collidable.add(world.getLevel().get(x,y));
					collis.add(world.getLevel().get(x,y));
				}
			}
		}
		return collis;
	}
}
